package com.example.guideapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.guideapp.R;
import com.example.guideapp.bean.Festival;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

/**
 * Created by Administrator on 2017/10/18.
 */

public class MyFestivalGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<Festival> list;

    private LayoutInflater mInflater;

    private Festival festival;

    public MyFestivalGridViewAdapter(Context context, List<Festival> list) {
        this.context = context;
        this.list = list;;

        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (list != null){
            return list.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        FestivalViewHolder viewHolder;

        if (convertView == null){
            convertView = mInflater.inflate(R.layout.fragment1_gridview_item,null);

            viewHolder = new FestivalViewHolder();
            viewHolder.festival_icon = (RoundedImageView) convertView.findViewById(R.id.iv1_festival_icon);
            viewHolder.festival_name = (TextView) convertView.findViewById(R.id.tv1_festival_name);
            viewHolder.festival_date = (TextView) convertView.findViewById(R.id.tv1_festival_date);
            viewHolder.festival_lunar = (TextView) convertView.findViewById(R.id.tv1_festival_lunar);
            viewHolder.festival_week = (TextView) convertView.findViewById(R.id.tv1_festival_week);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (FestivalViewHolder) convertView.getTag();
        }

        festival = list.get(position);

        byte[] icon = festival.getFestivalIcon();
        if (icon != null){
            Bitmap bm = BitmapFactory.decodeByteArray(icon,0,icon.length);
            viewHolder.festival_icon.setImageBitmap(bm);
        }else {
            viewHolder.festival_icon.setImageResource(R.mipmap.no);
        }

        viewHolder.festival_name.setText(festival.getFestivalName());
        viewHolder.festival_date.setText(festival.getFestivalDate());
        viewHolder.festival_lunar.setText(festival.getLunar());
        viewHolder.festival_week.setText(festival.getWeek());

        return convertView;
    }

     static class FestivalViewHolder{
        private RoundedImageView  festival_icon;
        private TextView festival_name;
         private TextView festival_date;
         private TextView festival_lunar;
         private TextView festival_week;
    }
}
